﻿<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta charset="utf-8" />
<link href="css/bootstrap.min.css" rel="stylesheet" />
<link href="css/font-awesome.min.css" rel="stylesheet" />
<link href="weixin/css/layer.css" rel="stylesheet" />
<script src="js/jquery-2.1.1.min.js"></script>
<script src="js/plugins/layer/layer.min.js"></script>
<script src="js/plugins/linq/linq.js"></script>
<script src="weixin/js/common.js"></script>
<script src="js/tableToExcel.js"></script>
<style>
    table {
        font-size: 15px;
        height: auto;
        width: auto;
        text-align: center!important;
    }
    table th {
        text-align: center!important;
        min-width: 100px;
        background-color: darkgrey;
    }
    .market{
        background-color: lightyellow;
    }
    .otherData {
        background-color: #c5d9f1;
    }
    .clossing {
        background-color: #f2dcdb;
    }
    .saleBillPartQty {
        background-color: #dcffd9;
    }
    .kpiQty {
        background-color: #fde9d9;
    }
    .nowData {
        background-color: #b7dee8;
    }
</style>
<html>
<head>
    <title>分部时段跟踪报表</title>
</head>
<body>
    <div style="margin-top: 5px; margin-left: 5px">
        <table border="1" id="report">
            <thead id="table_thead">
            </thead>
            <tbody id="body">
            </tbody>
        </table>
    </div>
</body>
</html>
<script type="text/javascript">
    $(function() {
        loadData();
        $(".btn-success").on("click", function () {
            loadData();
        });
    });
    function reflash() {
        loadData();
    };
    
    var loadData = function () {
        var s = layer.load(0,
            {
                shade: [0.2, '#fff'] //0.1透明度的白色背景
            });
        $.post("weixin/WXActivity/PostHandler.ashx",
            {
                "action": "TimeTrackingSalePartReport",
                "UserID": window.localStorage.getItem("UserID"),
            },
            function (data) {
                var tbody = $("#body");
                var table_thead = $("#table_thead");
                var thMark = "<tr class='market'>"
                thMark += "<td class='td-left'><b>数据更新时间为：每日12:15；15:15；18:15；21:15；24:15</b></td>";
                thMark += "<td ><a class='btn btn-success btn-xs btn-lg' onclick='reflash()'>刷新</a></td>";
                thMark += "<td ><a class='btn btn-success btn-xs btn-info' onclick='tableToExcelMethod(report)'>导出</a></td>";
                thMark += "</tr>";
                if (data.Success == true) {
                    if (data.Data.list.length > 0) {
                        var titleLength = data.Data.title.length;

                        thMark += "<tr class='table-title'>";
                        thMark +=
                            "<th rowspan='2'>省份</th><th rowspan='2'>区域经理</th><th rowspan='2'>店经理</th><th rowspan='2' style='min-width:250px'>店铺简称</th>";
                        thMark += "<th colspan='" + titleLength + "'>成交率</th>";
                        thMark += "<th colspan='" + titleLength + "'>单数</th>";
                        thMark += "<th colspan='" + titleLength + "'>客流量</th>";
                        thMark += "<th colspan='6'>指标</th>";
                        thMark += "</tr>";
                        thMark += "<tr id='table-title2'>";
                        data.Data.title.map(function (t) {
                            thMark += "<th id='Clossing_" + t.BillSegDate + "'>" + t.BillSegDateStr + "</th>";
                        });
                        data.Data.title.map(function (t) {
                            thMark += "<th id='SaleBillPartQty_" +
                                t.BillSegDate +
                                "'>" +
                                t.BillSegDateStr +
                                "</th>";
                        });
                        data.Data.title.map(function (t) {
                            thMark += "<th id='KpiQty_" + t.BillSegDate + "'>" + t.BillSegDateStr + "</th>";
                        });
                        thMark += "<th id='FactAMT_Date00tonow' class='date00tonow'>生意额</th>";
                        thMark += "<th id='UnitPrice_Date00tonow' class='date00tonow'>客单价</th>";
                        thMark += "<th id='SaleBillPartQty_Date00tonow' class='date00tonow'>单数</th>";
                        thMark += "<th id='Clossing_Date00tonow' class='date00tonow'>成交率</th>";
                        thMark += "<th id='SalelDv_Date00tonow' class='date00tonow'>连带率</th>";
                        thMark += "<th id='KpiQty_Date00tonow' class='date00tonow'>客流量</th>";
                        thMark += "</tr>";
                        table_thead.empty();
                        table_thead.append(thMark);

                        $(".td-left").attr("colspan", (4 + (titleLength * 3) + 4));

                        var thead = $("#table-title2").find("th");
                        var firstTH = Enumerable.From(thead)
                            .Where(function (x) {
                                return x.id.substring(0, 8) == "Clossing" && (x.className.indexOf("date00tonow") < 0)
                            }).ToArray();
                        var secondTH = Enumerable.From(thead)
                            .Where(function (x) {
                                return x.id.substring(0, 15) == "SaleBillPartQty" && (x.className.indexOf("date00tonow") < 0)
                            }).ToArray();
                        var thirdTH = Enumerable.From(thead)
                            .Where(function (x) {
                                return x.id.substring(0, 6) == "KpiQty" && (x.className.indexOf("date00tonow") < 0)
                            }).ToArray();

                        var tbHtml = "";

                        data.Data.list.map(function (li) {
                            var gLength = li.group.length;
                            var first = li.group[0];
                            var firstNow = Enumerable.From(first)
                                .Where(function (x) {
                                    return x.BillSegDate == "Date00tonow"
                                }).First();
                            var otherArr = li.group.slice(1);
                            var trtbFirst = "<tr>";
                            trtbFirst += "<td class='otherData' rowspan='" + gLength + "'>" + li.ProvinceName + "</td>";
                            trtbFirst += "<td class='otherData' rowspan='" + gLength + "'>" + li.Director + "</td>";
                            trtbFirst += "<td class='otherData' rowspan='" + gLength + "'>" + li.Manager + "</td>";
                            trtbFirst += "<td class='otherData'>" + first[0].ShopName + "</td>";
                            firstTH.map(function (th1) {
                                first.map(function (f1) {
                                    if (f1.BillSegDate == th1.id.substring(9, th1.id.length)) {
                                        trtbFirst += "<td class='clossing'>" +
                                            (Y.checkNull(f1.Clossing) ? ' ' : (f1.Clossing.toFixed(2) + "%")) +
                                            "</td>";
                                    }
                                });
                            });
                            secondTH.map(function (th2) {
                                first.map(function (f2) {
                                    if (f2.BillSegDate == th2.id.substring(16, th2.id.length)) {
                                        trtbFirst += "<td class='saleBillPartQty'>" +
                                            (Y.checkNull(f2.SaleBillQty) ? ' ' : (f2.SaleBillQty)) +
                                            "</td>";
                                    }
                                });
                            });
                            thirdTH.map(function (th3) {
                                first.map(function (f3) {
                                    if (f3.BillSegDate == th3.id.substring(7, th3.id.length)) {
                                        trtbFirst += "<td class='kpiQty'>" + (Y.checkNull(f3.KpiQty) ? ' ' : (f3.KpiQty)) + "</td>";
                                    }
                                });
                            });
                            trtbFirst += "<td class='nowData'>" + (Y.checkNull(firstNow.FactAMT) ? ' ' : (firstNow.FactAMT)) + "</td>";
                            trtbFirst += "<td class='nowData'>" + (Y.checkNull(firstNow.UnitPrice) ? ' ' : (firstNow.UnitPrice.toFixed(0))) + "</td>";
                            trtbFirst += "<td class='nowData'>" + (Y.checkNull(firstNow.SaleBillQty) ? ' ' : (firstNow.SaleBillQty)) + "</td>";
                            trtbFirst += "<td class='nowData'>" + (Y.checkNull(firstNow.Clossing) ? ' ' : (firstNow.Clossing.toFixed(1) + "%")) + "</td>";
                            trtbFirst += "<td class='nowData'>" + (Y.checkNull(firstNow.SalelDv) ? ' ' : (firstNow.SalelDv.toFixed(2))) + "</td>";
                            trtbFirst += "<td class='nowData'>" + (Y.checkNull(firstNow.KpiQty) ? ' ' : (firstNow.KpiQty)) + "</td>";
                            trtbFirst += "</tr>";
                            tbHtml += trtbFirst;
                            otherArr.map(function (oa) {
                                var oaNow = Enumerable.From(oa)
                                    .Where(function (x) {
                                        return x.BillSegDate == "Date00tonow"
                                    }).First();
                                var trtbOther = "<tr>";
                                trtbOther += "<td class='otherData'>" + oa[0].ShopName + "</td>";
                                firstTH.map(function (thO1) {
                                    oa.map(function (o1) {
                                        if (o1.BillSegDate == thO1.id.substring(9, thO1.id.length)) {
                                            trtbOther += "<td class='clossing'>" + (Y.checkNull(o1.Clossing) ? ' ' : (o1.Clossing.toFixed(2) + "%")) + "</td>";
                                        }
                                    });
                                });
                                secondTH.map(function (thO2) {
                                    oa.map(function (o2) {
                                        if (o2.BillSegDate == thO2.id.substring(16, thO2.id.length)) {
                                            trtbOther += "<td class='saleBillPartQty'>" + (Y.checkNull(o2.SaleBillQty) ? ' ' : (o2.SaleBillQty)) + "</td>";
                                        }
                                    });
                                });
                                thirdTH.map(function (thO3) {
                                    oa.map(function (o3) {
                                        if (o3.BillSegDate == thO3.id.substring(7, thO3.id.length)) {
                                            trtbOther += "<td class='kpiQty'>" + (Y.checkNull(o3.KpiQty) ? ' ' : (o3.KpiQty)) + "</td>";
                                        }
                                    });
                                });
                                trtbOther += "<td class='nowData'>" + (Y.checkNull(oaNow.FactAMT) ? ' ' : (oaNow.FactAMT)) + "</td>";
                                trtbOther += "<td class='nowData'>" + (Y.checkNull(oaNow.UnitPrice) ? ' ' : (oaNow.UnitPrice.toFixed(0))) + "</td>";
                                trtbOther += "<td class='nowData'>" + (Y.checkNull(oaNow.SaleBillQty) ? ' ' : (oaNow.SaleBillQty)) + "</td>";
                                trtbOther += "<td class='nowData'>" + (Y.checkNull(oaNow.Clossing) ? ' ' : (oaNow.Clossing.toFixed(1) + "%")) + "</td>";
                                trtbOther += "<td class='nowData'>" + (Y.checkNull(oaNow.SalelDv) ? ' ' : (oaNow.SalelDv.toFixed(2) )) + "</td>";
                                trtbOther += "<td class='nowData'>" + (Y.checkNull(oaNow.KpiQty) ? ' ' : (oaNow.KpiQty)) + "</td>";
                                trtbOther += "</tr>";
                                tbHtml += trtbOther;
                            });
                        });
                        tbody.empty();
                        tbody.append(tbHtml);
                    } else {
                        table_thead.empty();
                        table_thead.append(thMark);
                        var emptyTable = "<tr><td colspan='15'>暂无数据</td></tr>";
                        tbody.empty();
                        tbody.append(emptyTable);
                    }
                    layer.close(s);
                } else {
                    table_thead.empty();
                    table_thead.append(thMark);
                    layer.close(s);
                }
            });
    }

</script>
